package com.richinfo.service;

import com.richinfo.mapper.SysuserMapper;
import com.richinfo.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@SuppressWarnings("rawtypes")
public class SysUserService {
    private static Logger logger = Logger.getLogger(SysUserService.class);

    @Autowired
    private SysuserMapper sysuserMapper;

    public List<Map<String, Object>> getUserByMobile(Map params) {
        List<Map<String, Object>> result = new ArrayList<>();
        Map userMap = new HashMap();
        try {
            userMap = sysuserMapper.getUser(params);
            logger.info("******根据手机号查询到用户："+userMap);
            Map<String, Object> user = new HashMap<>();
            if(null!=userMap && userMap.size()>0){
                user.put("id", userMap.get("id"));
                user.put("sdk_pwd", userMap.get("sdk_pwd"));
                user.put("name", userMap.get("name"));
                user.put("sex", userMap.get("sex"));
                user.put("company_id", userMap.get("company_id"));
                user.put("office_id", userMap.get("office_id"));
                user.put("login_name", userMap.get("login_name"));
                user.put("mobile", (String) userMap.get("mobile"));
                user.put("company_name", userMap.get("company_name"));
                user.put("del_flag", userMap.get("del_flag"));
                user.put("photo", userMap.get("photo"));
                user.put("oaToken", "");
                user.put("portalToken", "");
                user.put("rootOfficeName", "");
                user.put("jobType", userMap.get("job_type"));
                String position = (String) userMap.get("position");

                // 解析职位信息
                if (position != null && position != "") {
                    String[] pdes = position.split("\\|");
                    if (pdes.length > 1) {
                        position = pdes[1];
                    }
                }
                if (null != position && StringUtil.isNotEmpty(position)) {
                    user.put("position", position);
                } else {
                    user.put("position", "");
                }
                user.put("phone", userMap.get("phone"));
                user.put("email", userMap.get("email"));
                if (null != userMap.get("priority")) {
                    user.put("priority", userMap.get("priority"));
                } else {
                    user.put("priority", "");
                }
                user.put("officeName", userMap.get("officeName"));
                user.put("rootOfficeId", userMap.get("rootOfficeId"));
                result.add(user);
            }else{
                logger.error("************该手机号码暂无对应的用户！！***************");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (List<Map<String, Object>>) result;
    }
}